package com.blindnews.controller;

import com.blindnews.pojo.Channel;
import com.blindnews.pojo.News;
import com.blindnews.pojo.NewsParam;
import com.blindnews.pojo.Result;
import com.blindnews.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.io.IOException;
import java.util.*;


//新闻
@Slf4j
@CrossOrigin
@RestController
public class NewsController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private RestTemplate restTemplate;
//从API中获得新闻列表信息，存入数据库中
    @GetMapping("/downloadNews")
    public Result downloadNews(@RequestBody NewsParam newsParam) throws IOException {
        //获取请求参数
        //拼接请求参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("channelId",newsParam.getChannelId());
        paramMap.put("title",newsParam.getTitle());
        paramMap.put("page",newsParam.getPage());
        paramMap.put("maxResult",newsParam.getMaxResult());
        paramMap.put("id",newsParam.getId());
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("Authorization", "APPCODE 5507c2e66bef4d4cb6dfc9a2ad9bdfee");
        HttpEntity request = new HttpEntity(headers);
        //请求url
        String url = "https://news1.market.alicloudapi.com/newsList?channelId={channelId}&title={title}&page={page}&maxResult={maxResult}&id={id}";
        ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.GET,request,String.class,paramMap);
        Boolean data=newsService.downloadnews(response);
        if(data){
            return Result.success("success",data);

        }
        return Result.error("error");

    }
//从API中获得新闻频道列表，存入数据库中
    @GetMapping("/downloadChannel")
    public Result downloadChannel( ){
//        请求url
        String url="https://news1.market.alicloudapi.com/channelList";
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("Authorization", "APPCODE 5507c2e66bef4d4cb6dfc9a2ad9bdfee");
        HttpEntity request = new HttpEntity(headers);

        ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.GET,request,String.class);
        Boolean data=newsService.downloadchannel(response);
        if(data){
            return Result.success("success",data);
        }
        return Result.error("error");
    }
//    从数据库中获得频道信息，渲染页面
    @GetMapping("/getChannel")
    public Result getChannel(){
        List<Channel> channel = newsService.getChannel();
        return Result.success("success",channel);
    }
//    从数据库中获得新闻列表信息，渲染页面
    @GetMapping("/getNewsList")
    public Result getNewsList(String channelId){
        List<News> newsList = newsService.getNewsList(channelId);
        return Result.success("success",newsList);
    }
//     前台点击某条新闻时,从数据库中获得新闻信息，渲染页面
    @GetMapping("/upLoadNews")
    public Result upLoadNews( String id){
        News news=newsService.upLoadNews(id);
        return Result.success("success",news);
    }
//    @GetMapping("/getNewsInfo")
//    public Result getNewsInfo(){
//        String url = "https://www.163.com/dy/article/ITSDI3OV0514R9P4.html";
//        newsService.getNewsInfo(url);
//            return Result.success();
//    }

//
//    @GetMapping("/findNews")
//    public Result findnews() {
//        String channelId="5572a108b3cdc86cf39001cd";
//        String title=null;
//        String page="1";
//        String maxResult="50";
//        String id=null;
//
//
//
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("channelId",channelId);
//        paramMap.put("title",title);
//        paramMap.put("page",page);
//        paramMap.put("maxResult",maxResult);
//        paramMap.put("id",id);
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
//        headers.add("Authorization", "APPCODE 5507c2e66bef4d4cb6dfc9a2ad9bdfee");
//
//
//
//        HttpEntity request = new HttpEntity(headers);
//
//
//        String url = "https://news1.market.alicloudapi.com/newsList?channelId={channelId}&title={title}&page={page}&maxResult={maxResult}&id={id}";
//
//
//        ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.GET,request,String.class,paramMap);
//        if (response.getStatusCode() == HttpStatus.OK) {
//            System.out.println("Request Successful.");
//            System.out.println(response.getBody());
//            //解析返回的数据
//            JSONObject jsTemp = JSONObject.parseObject(response.getBody());
//            JSONObject jsonObject =jsTemp.getJSONObject("showapi_res_body");
//            JSONObject jsonObject1=jsonObject.getJSONObject("pagebean");
//            int JSON_allnum=jsonObject1.getIntValue("allNum");
//            int JSON_maxResult=jsonObject1.getIntValue("maxResult");
//            int JSON_currentPage=jsonObject1.getIntValue("currentPage");
//            int JSON_allPages=jsonObject1.getIntValue("allPages");
//            System.out.println(JSON_allnum);
//            System.out.println(JSON_maxResult);
//            System.out.println(JSON_currentPage);
//            System.out.println(JSON_allPages);
//
//            JSONArray contentlist=jsonObject1.getJSONArray("contentlist");
//
//            System.out.println(contentlist);
//
//            JSONObject content = null;
//            List<News> newsList=new ArrayList<>();
//            for (int i = 0; i < JSON_maxResult; i++) {
//                System.out.println("进入循环");
//                content = contentlist.getJSONObject(i);
//                News item=new News();
//                item.setChannelId(content.get("channelId").toString());
//                item.setLink(content.get("link").toString());
//                item.setId(content.get("id").toString());
//                item.setSource(content.get("source").toString());
//                item.setTitle(content.get("title").toString());
//                item.setPubDate(content.get("pubDate").toString());
//                item.setChannelName(content.get("channelName").toString());
//                newsList.add(item);
//                System.out.println(item.toString());
//            }
//
//            System.out.println(newsList);
//            newsService.addNewsList(newsList);
//            System.out.println("控制层");
//
//            return Result.success("success",contentlist);
//        } else {
//            System.out.println("Request Failed");
//            System.out.println(response.getStatusCode());
//            return Result.error("error");
//        }
//
//
//
//    }

    //
//    @PostMapping ("/getChannel")
//    public Result channellist(@RequestBody List<Channel> channelList){
//        newsService.addChannel(channelList);
//        System.out.println("控制层");
//        return Result.success();
//    }
//    @PostMapping("/getNews")
//    public Result newsList(@RequestBody List<News> newsList){
//        newsService.addNewsList(newsList);
//        System.out.println("控制层");
//        return Result.success();
//    }
}
